package com.itstyle.quartz.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.itstyle.quartz.entity.JobLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.itstyle.quartz.dynamicquery.DynamicQuery;
import com.itstyle.quartz.entity.QuartzEntity;
import com.itstyle.quartz.service.IJobService;

@Service("jobService")
public class JobServiceImpl implements IJobService {

    @Autowired
    private DynamicQuery dynamicQuery;

    @Override
    public List<QuartzEntity> listQuartzEntity(QuartzEntity quartz,
                                               Integer pageNo, Integer pageSize) {
        //PageHelper.startPage(pageNo, pageSize);
        StringBuffer nativeSql = new StringBuffer();
        nativeSql.append("SELECT job.job_data as jobData,job.JOB_NAME as jobName,job.JOB_GROUP as jobGroup,job.DESCRIPTION as description,job.JOB_CLASS_NAME as jobClassName,");
        nativeSql.append("cron.CRON_EXPRESSION as cronExpression,tri.TRIGGER_NAME as triggerName,tri.TRIGGER_STATE as triggerState,");
        nativeSql.append("job.JOB_NAME as oldJobName,job.JOB_GROUP as oldJobGroup ");
        nativeSql.append("FROM QRTZ_JOB_DETAILS AS job LEFT JOIN QRTZ_TRIGGERS AS tri ON job.JOB_NAME = tri.JOB_NAME ");
        nativeSql.append("LEFT JOIN QRTZ_CRON_TRIGGERS AS cron ON cron.TRIGGER_NAME = tri.TRIGGER_NAME ");
        nativeSql.append("WHERE tri.TRIGGER_TYPE = 'CRON'");
        Object[] params = new Object[]{};
        if (!StringUtils.isEmpty(quartz.getJobName())) {//加入JobName搜索其他条件自行实现
            nativeSql.append(" AND job.JOB_NAME like ?");
            params = new Object[]{"%" + quartz.getJobName() + "%"};
        }
        nativeSql.append(" order by create_time desc limit " + pageSize);
        return dynamicQuery.nativeQueryListModel(QuartzEntity.class, nativeSql.toString(), params);
    }

    @Override
    public Long listQuartzEntity(QuartzEntity quartz) {
        StringBuffer nativeSql = new StringBuffer();
        nativeSql.append("SELECT COUNT(*)");
        nativeSql.append("FROM QRTZ_JOB_DETAILS AS job LEFT JOIN QRTZ_TRIGGERS AS tri ON job.JOB_NAME = tri.JOB_NAME ");
        nativeSql.append("LEFT JOIN QRTZ_CRON_TRIGGERS AS cron ON cron.TRIGGER_NAME = tri.TRIGGER_NAME ");
        nativeSql.append("WHERE tri.TRIGGER_TYPE = 'CRON'");
        return dynamicQuery.nativeQueryCount(nativeSql.toString(), new Object[]{});
    }

    @Override
    public Integer saveJobLog(JobLog log) {
        dynamicQuery.save(log);
        return 1;
    }

    @Override
    public List<JobLog> listJobLog(JobLog jobLog,
                                   Integer pageNo, Integer pageSize) {
        //PageHelper.startPage(pageNo, pageSize);
        StringBuffer nativeSql = new StringBuffer();
        nativeSql.append("select id,job_name as jobName,job_url as jobUrl,job_status as jobStatus,job_result as jobResult,create_time as createTime");
        nativeSql.append(" from job_log");
        Object[] params = new Object[]{};
        if (!StringUtils.isEmpty(jobLog.getJobName())) {
            nativeSql.append(" where job_name = ? ");
            params = new Object[]{jobLog.getJobName()};
        }

        nativeSql.append(" order by id desc limit " + pageSize);
        return dynamicQuery.nativeQueryListModel(JobLog.class, nativeSql.toString(), params);
    }
}
